package ie.dkit.semanticPhotoWeb.server.googleAPIHandler;

import java.io.IOException;
import java.util.logging.Logger;

import com.google.api.gbase.client.FeedURLFactory;
import com.google.api.gbase.client.GoogleBaseEntry;
import com.google.api.gbase.client.GoogleBaseFeed;
import com.google.api.gbase.client.GoogleBaseQuery;
import com.google.api.gbase.client.GoogleBaseService;
import com.google.api.gbase.client.ServiceError;
import com.google.api.gbase.client.ServiceErrors;
import com.google.gdata.util.ServiceException;

/**
 * Currently unused, maybe better for later releases
 * 
 * @author Sebastian Ruehl
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA
 */
public class BaseAPIHandler extends APIHandler {

	private final static Logger logger = Logger.getLogger(BaseAPIHandler.class
			.getName());

	private static FeedURLFactory urlFactory = FeedURLFactory.getDefault();

	public BaseAPIHandler() {
		super(new GoogleBaseService(APP_ID));
	}

	public void query(String queryString) {
		// Create a query URL from the given arguments
		GoogleBaseQuery query = new GoogleBaseQuery(urlFactory
				.getSnippetsFeedURL());
		query.setGoogleBaseQuery(queryString);
		query.setResultFormat(GoogleBaseQuery.ResultFormat.ATOM);

		// Display the URL generated by the API
		logger.info("Sending request to: " + query.getUrl());

		try {
			GoogleBaseFeed feed;
			feed = service.query(query, GoogleBaseFeed.class);

			// Print the items
			printResult(feed);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ServiceException e) {
			printServiceException(e);
		}
	}

	// Helper Methods

	private void printResult(GoogleBaseFeed feed) {
		if (feed.getTotalResults() == 0) {
			logger.info("No matches.");
		} else {
			for (GoogleBaseEntry entry : feed.getEntries()) {
				logger.info(entry.getGoogleBaseAttributes().getItemType()
						+ ": " + entry.getTitle().getPlainText() + " - "
						+ entry.getId());
			}
		}
	}

	private void printServiceException(ServiceException e) {
		StringBuilder errorString = new StringBuilder();
		errorString.append("Error");
		if (e.getHttpErrorCodeOverride() > 0) {
			errorString.append("" + e.getHttpErrorCodeOverride());
		}
		errorString.append(": " + e.getMessage());

		ServiceErrors errors = new ServiceErrors(e);
		for (ServiceError error : errors.getAllErrors()) {
			String field = error.getField();
			if (field != null) {
				errorString.append("in field '" + field + "' ");
			} else {
				errorString.append(error.getReason());
			}
		}
		logger.warning(errorString.toString());
	}
}
